package com.fuhe.chen.vendingmachine.controller.admin;

import com.alibaba.fastjson.JSON;
import com.fuhe.chen.vendingmachine.anno.AddLog;
import com.fuhe.chen.vendingmachine.pojo.CommoditySellLabel;
import com.fuhe.chen.vendingmachine.service.ICommoditySellLabelService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

@Controller
@Api(tags = "商品营销标签管理")
@RequestMapping("/admin/commoditySellLabel")
public class CommoditySellLabelController {

    @Autowired
    ICommoditySellLabelService sellLabelService;

    @ApiOperation(value = "获取全部营销标签", notes="返回json格式数据")
    @GetMapping({""})
    @ResponseBody
    public String getAllLabel(){
        List<CommoditySellLabel> all = sellLabelService.findAll();
        return JSON.toJSONString(all);
    }

    @ApiOperation(value = "添加营销标签", notes="返回添加结果")
    @PostMapping({"/addLabel"})
    @ResponseBody
    @AddLog(operation = "添加营销标签")
    public String addLabel(String title,String subtitle){
        CommoditySellLabel commoditySellLabel = new CommoditySellLabel();
        commoditySellLabel.setTitle(title);
        commoditySellLabel.setSubtitle(subtitle);
        String msg = sellLabelService.addLabel(commoditySellLabel);
        return msg;
    }

    @ApiOperation(value = "删除营销标签", notes="返回删除结果")
    @PostMapping({"/delLabel"})
    @ResponseBody
    @AddLog(operation = "删除营销标签")
    public String delLabel(Integer labelId){
        sellLabelService.deleteLabel(labelId);
        return "删除成功";
    }

    @ApiOperation(value = "绑定商品和营销标签", notes="返回绑定结果")
    @PostMapping({"/bindCommodity"})
    @ResponseBody
    @AddLog(operation = "绑定商品和营销标签")
    public String bindCommodity(Integer commodityId,Integer labelId){
        String msg = sellLabelService.bindCommodity(commodityId,labelId);
        return msg;
    }

    @ApiOperation(value = "解除商品和营销标签的绑定", notes="返回解除结果")
    @PostMapping({"/unBindCommodity"})
    @ResponseBody
    @AddLog(operation = "解除商品和营销标签的绑定")
    public String unBindCommodity(Integer commodityId,Integer labelId){
        Integer resultRow = sellLabelService.unBindCommodity(commodityId, labelId);
        if(resultRow == 0){
            return  "解除绑定失败,不存在绑定关系";
        }
        return  "解除绑定成功";
    }

}
